package tasks

import (
	"github.com/astaxie/beego"
	"github.com/gocelery/gocelery"
	"github.com/gomodule/redigo/redis"
	"time"
)

//gocelery定义客户端
func Client() *gocelery.CeleryClient{
	task_broker := beego.AppConfig.String("task_broker")
	redisPool := &redis.Pool{
		MaxIdle:     16,                 // maximum number of idle connections in the pool
		MaxActive:   0,                 // maximum number of connections allocated by the pool at a given time
		IdleTimeout: 300, 				// close connections after remaining idle for this duration
		Dial: func() (redis.Conn, error) {
			c, err := redis.DialURL(task_broker)
			if err != nil {
				return nil, err
			}
			return c, err
		},
		TestOnBorrow: func(c redis.Conn, t time.Time) error {
			_, err := c.Do("PING")
			return err
		},
	}

	// initialize celery client
	cli, _ := gocelery.NewCeleryClient(
		gocelery.NewRedisBroker(redisPool),
		&gocelery.RedisCeleryBackend{Pool: redisPool},
		1,
	)
	return cli
}
